package imlayer

import (
	"encoding/json"
	"dhfshop/models/isv"
	"github.com/astaxie/beego"
	"wxsdk/miniapp/message/custom/miniapp"
	"wxsdk/miniapp/message/custom/public"
)

func SendCustomerMessage(appId, openId, msgType, msgBody string) error {
	custAccount, err := isv.GetCustAccountByAppId(appId)
	if err != nil {
		beego.Emergency("WS ReadMessage GetCustAccountByAppId Err:", err)
		return err
	}
	if custAccount.ServiceTypeId == 1 {
	   SendPubCustomerMessage(appId,custAccount.AccessToken,openId,msgType,msgBody)
	}else{
	   SendAppCustomerMessage(appId,custAccount.AccessToken,openId,msgType,msgBody)
	}
	return nil
}

// service agent send messages to customer
func SendAppCustomerMessage(appId, accessToken, openId, msgType, msgBody string) error {
	switch msgType {
		case "text":
			body := struct {
				Content string `json:"content"` 
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := miniapp.SendTextMessage(accessToken,openId,body.Content); err != nil {
					beego.Emergency("SendTextMessage Err:", err)
					return err
				}
			}
		case "image":
			body := struct {
				MediaId string `json:"media_id"`
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := miniapp.SendImageMessage(accessToken,openId,body.MediaId); err != nil {
					beego.Emergency("SendImageMessage Err:", err)
					return err
				}
			}
		case "link":
		    body := struct {
				Title       string `json:"title"`
				Description string `json:"description"`
				Url         string `json:"url"`
				ThumbUrl    string `json:"thumb_url"`
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := miniapp.SendLinkMessage(accessToken,openId,body.Title,body.Description,body.Url,body.ThumbUrl); err != nil {
					beego.Emergency("SendLinkMessage Err:", err)
					return err
				}
			}
		case "miniprogrampage":
		    body := struct {
				Title        string `json:"title"`
				PagePath     string `json:"page_path"`
				ThumbMediaId string `json:"thumb_media_id"`
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := miniapp.SendMiniProgramPageMessage(accessToken,openId,body.Title,body.PagePath,body.ThumbMediaId); err != nil {
					beego.Emergency("SendMiniProgramPageMessage Err:", err)
					return err
				}
			}
		case "qrcode":
			body := struct {
				Content string `json:"content"` 
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := miniapp.SendTextMessage(accessToken,openId,body.Content); err != nil {
					beego.Emergency("SendTextMessage Err:", err)
					return err
				}
			}
		default:
			
	}
	return nil
}

func SendPubCustomerMessage(appId, accessToken, openId, msgType, msgBody string) error {
	switch msgType {
		case "text":
			body := struct {
				Content string `json:"content"` 
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendTextMessage(accessToken,openId,body.Content); err != nil {
					beego.Emergency("SendTextMessage Err:", err)
					return err
				}
			}
		case "image":
			body := struct {
				MediaId string `json:"media_id"`
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendImageMessage(accessToken,openId,body.MediaId); err != nil {
					beego.Emergency("SendImageMessage Err:", err)
					return err
				}
			}
		case "voice":
			body := struct {
				MediaId string `json:"media_id"`
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendVoiceMessage(accessToken,openId,body.MediaId); err != nil {
					beego.Emergency("SendVoiceMessage Err:", err)
					return err
				}
			}	
		case "video":
			body := struct {
				MediaId      string `json:"media_id"`              // 通过素材管理接口上传多媒体文件得到 MediaId
				ThumbMediaId string `json:"thumb_media_id"`        // 通过素材管理接口上传多媒体文件得到 ThumbMediaId
				Title        string `json:"title,omitempty"`       // 视频消息的标题, 可以为 ""
				Description  string `json:"description,omitempty"` // 视频消息的描述, 可以为 ""
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendVideoMessage(accessToken,openId,body.MediaId,
				                    body.ThumbMediaId,body.Title,body.Description); err != nil {
					beego.Emergency("SendVideoMessage Err:", err)
					return err
				}
			}	
		case "music":
			body := struct {
				Title        string `json:"title,omitempty"`       // 音乐标题, 可以为 ""
				Description  string `json:"description,omitempty"` // 音乐描述, 可以为 ""
				MusicURL     string `json:"musicurl"`              // 音乐链接
				HQMusicURL   string `json:"hqmusicurl"`            // 高质量音乐链接, WIFI环境优先使用该链接播放音乐
				ThumbMediaId string `json:"thumb_media_id"`        // 通过素材管理接口上传多媒体文件得到 ThumbMediaId
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendMusicMessage(accessToken,openId,body.ThumbMediaId,
				                    body.MusicURL,body.HQMusicURL,body.Title,body.Description); err != nil {
					beego.Emergency("SendMusicMessage Err:", err)
					return err
				}
			}
		case "news":
			body := struct {
				Articles []public.Article `json:"articles,omitempty"` // 多条图文消息信息
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendNewsMessage(accessToken,openId,body.Articles); err != nil {
					beego.Emergency("SendNewsMessage Err:", err)
					return err
				}
			}	
		case "mpnews":
			body := struct {
				MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendMpNewsMessage(accessToken,openId,body.MediaId); err != nil {
					beego.Emergency("SendMpNewsMessage Err:", err)
					return err
				}
			}	
		case "wxcard":
			body := struct {
				CardId  string `json:"card_id"`
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendWxCardMessage(accessToken,openId,body.CardId,""); err != nil {
					beego.Emergency("SendWxCardMessage Err:", err)
					return err
				}
			}		
		case "miniprogrampage":
		    body := struct {
				Title        string `json:"title"`
				PagePath     string `json:"page_path"`
				ThumbMediaId string `json:"thumb_media_id"`
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				if _, err := public.SendMiniProgramPageMessage(accessToken,openId,body.Title,appId,body.PagePath,body.ThumbMediaId); err != nil {
					beego.Emergency("SendMiniProgramPageMessage Err:", err)
					return err
				}
			}
		default:
			
	}
	return nil
}

